home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / amusemen / vgatarot.000 / vgatarot / VGATarot-0.3 / vgaaces.c < prev    next >
C/C++ Source or Header  |  1994-05-24  |  7KB  |  246 lines

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<strings.h>
  4. #include<allinc.h>
  5. #include<unistd.h>
  6. #include<signal.h>
  7. #include<vga.h>
  8. #include<vgagl.h>
  9. #include<vgakeyboard.h>
  10. #include<sys/time.h>
  11. #include<sys/types.h>
  12. #include<fcntl.h>
  13. #include"zsoftpcx.h"
  14.  
  15. typedef struct timeval t_tv;
  16. typedef struct timezone t_tz;
  17.     
  18. unsigned long giverand(unsigned long range,int seedadd);
  19.  
  20. void viewpcx(int XBeg,int YBeg,char *libname,char *indexname,int cnum,int orientation);
  21.  
  22. void north_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  23.  
  24. void east_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  25.  
  26. void south_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);
  27.  
  28. void west_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel);        
  29.  
  30.  
  31.  
  32. void main(int argc,char *argv[]) {
  33.     char question[5000];
  34.     char name[200];
  35.     int hand[4][2] = {{0,0},{0,0},{0,0},{0,0}};
  36.     int done=0;
  37.     int deckx[78] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  38.              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  39.              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };    
  40.     int vgamode=G640x480x256;
  41.     char posit[4][80] = {"First","Second","Third","Fourth"};
  42.  
  43.     int pos[4][2]={{48,160},{196,160},{344,160},{492,160}};
  44.     int seed,x,y,z;
  45.  
  46.     printf("\nVGATarot - Aces Spread V0.3\n");
  47.     printf("\nPlease enter your name (first and last) > ");
  48.     gets(name);
  49.     printf("Please enter your question\n> ");
  50.     gets(question);
  51.     
  52.     for(x=0,seed=0;x!=strlen(name);x++,seed+=name[x]);
  53.     for(x=0;x!=strlen(question);x++,seed+=question[x]);
  54.     
  55.     for(z=0;z!=4;z++) {
  56.     done=0;
  57.     y=0;
  58.     while(!done) {
  59.         x=(int)giverand(78,seed);
  60.         if(deckx[x]==0) {
  61.             deckx[x]=1;
  62.             y++;
  63.             if((x==22)||(x==36)||(x==50)||(x==64)) done = 1;
  64.             if(y==13) done = 1; }
  65.         hand[z][0]=x;
  66.         if((int)giverand(100,seed)>50) hand[z][1]=1; else hand[z][1]=0; } }
  67.  
  68.     if(!vga_hasmode(vgamode)) {
  69.         printf("mode is unsupported\n"); 
  70.         exit(-1); }
  71.         
  72.     vga_setmode(vgamode);
  73.     gl_setcontextvga(vgamode);
  74.     gl_enableclipping();
  75.     gl_setwritemode(WRITEMODE_OVERWRITE);
  76.     
  77.     printf("\n");
  78.  
  79.     for(x=0;x!=4;x++) {
  80.         if(hand[x][1]==0) seed=1; else seed=3;
  81.         printf("%-20s : %s %s\n",posit[x],deck[hand[x][0]],hand[x][1]==0?"":"Reversed");
  82.         viewpcx(pos[x][0],pos[x][1],"tarotlib.lbr","tarotlib.index",hand[x][0],seed); }
  83.         
  84.     printf("\n");
  85.  
  86.     getchar();
  87.     vga_setmode(TEXT);
  88.     
  89.     exit(1); }
  90.     
  91. unsigned long giverand(unsigned long range,int seedadd)
  92.     { 
  93.     t_tv tv;
  94.     t_tz tz;
  95.         
  96.     gettimeofday(&tv,&tz);
  97.     srandom((unsigned int)tv.tv_usec+(unsigned int)seedadd);
  98.     return (unsigned long)((random()%range)+1); }
  99.  
  100. void viewpcx(int XBeg,int YBeg,char *libname,char *indexname,int cnum,int orientation) { 
  101.     int fi; /* index file */
  102.     int fp; /* data file */
  103.     unsigned char *data_pix;
  104.     unsigned long foffset;
  105.     unsigned long flength;
  106.     int XPos,YPos,XMax,YMax;
  107.     zhead pcx;
  108.     Palette pal;
  109.     unsigned long x;
  110.     unsigned int y,z,inbyte;
  111.     unsigned char inc;
  112.  
  113.     if((fi=open(indexname,O_RDONLY))==-1) {
  114.         vga_setmode(TEXT);
  115.         printf("cannot open index file\n");
  116.         exit(-1); }
  117.         
  118.     lseek(fi,cnum*8,SEEK_SET); /* get the correct index */
  119.     
  120.     read(fi,&foffset,sizeof(foffset)); /* get the offset */
  121.     read(fi,&flength,sizeof(flength)); /* get the length */
  122.     
  123.     close(fi); /* close index file */
  124.     
  125.     if((fp=open(libname,O_RDONLY))==-1) { /* open library file */
  126.         vga_setmode(TEXT);
  127.         printf("cannot open library file\n");
  128.         exit(-1); }
  129.     
  130.     lseek(fp,foffset,SEEK_SET); /* go to correct offset in library */
  131.             
  132.     if(read(fp,&pcx,sizeof(pcx))!=sizeof(pcx)) { /* read PCX header information */
  133.         vga_setmode(TEXT);
  134.         printf("cannot read header information of picture %d\n",cnum);
  135.         exit(-1); }
  136.         
  137.     XMax=(int)pcx.x2; YMax=(int)pcx.y2; /* set the x and y dimensions of the picture */
  138.     
  139.     lseek(fp,128-sizeof(pcx),SEEK_CUR);
  140.     
  141.     if((data_pix=(char *)malloc(flength-128))==NULL) { /* get picture memory */
  142.         vga_setmode(TEXT);
  143.         printf("couldn't allocate picture data memory\n");
  144.         exit(-1); }    
  145.         
  146.     if(((unsigned long)read(fp,data_pix,(flength-128)))!=(flength-128)) { /* read picture data */
  147.         vga_setmode(TEXT);
  148.         printf("couldn't read in picture data\n");
  149.         exit(-1); }
  150.         
  151.     close(fp); /* close library file */
  152.     
  153.     data_pix+=(flength-128-768); /* get to the palette data */
  154.                 
  155.     for(x=0;x<255;x++) { /* set the palette data */
  156.         inc=*data_pix++;
  157.         pal.color[x].red=(inc/4);
  158.         inc=*data_pix++;
  159.         pal.color[x].green=(inc/4);
  160.         inc=*data_pix++;
  161.         pal.color[x].blue=(inc/4); }
  162.         
  163.     data_pix-=(flength-131); /* why 131?  I've no damn clue, but it works better than 128 */
  164.         
  165.     gl_setpalette(&pal); /* set the palette */
  166.     
  167.     switch(orientation) { /* set up correct orientation parameters */
  168.         case 1 : /* north */ 
  169.                     XMax=(int)pcx.x2+1;
  170.                     YMax=(int)pcx.y2;
  171.                     XPos=XBeg; 
  172.                     YPos=YBeg; 
  173.                     break;
  174.         case 2 : /* east */ 
  175.                     XMax=(int)pcx.x2+1;
  176.                     YMax=(int)pcx.y2;
  177.                     XPos=YBeg+YMax;
  178.                     YPos=XBeg;
  179.                     break;
  180.         case 3 : /* south */ 
  181.                     XMax=(int)pcx.x2+1;
  182.                     YMax=(int)pcx.y2;
  183.                     XPos=XBeg+XMax; 
  184.                     YPos=YBeg+YMax; 
  185.                     break;
  186.         case 4 : /* west */ 
  187.                     XMax=(int)pcx.x2+1;
  188.                     YMax=(int)pcx.y2;
  189.                     XPos=YBeg;
  190.                     YPos=XBeg+XMax;
  191.                     break; }
  192.     
  193.     for(x=0;x<(XMax*YMax);) { /* decode the picture */
  194.         inbyte=(unsigned int)*data_pix++;
  195.         if(inbyte>0xc0) { /* RLE compression */
  196.             inbyte-=0xc0;
  197.             y=(unsigned int)*data_pix++;
  198.             for(z=0;z<inbyte;z++) {
  199.                 x++;
  200.                 switch(orientation) {
  201.                     case 1 : north_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;
  202.                     case 2 : east_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;                    
  203.                     case 3 : south_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break;
  204.                     case 4 : west_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,y); break; } } }
  205.         else {
  206.             x++;
  207.             switch(orientation) {
  208.                 case 1 : north_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;
  209.                 case 2 : east_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;                                    
  210.                 case 3 : south_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break;
  211.                 case 4 : west_pixel(&XPos,&YPos,XMax,YMax,XBeg,YBeg,inbyte); break; } } }
  212.                 
  213.         free(data_pix); } /* free the data, we are done */
  214.  
  215. void north_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  216.     if(*y>(ym+yb)) return;
  217.     gl_setpixel(*x,*y,pixel);
  218.     *x=*x+1;
  219.     if(*x==(xm+xb)) {
  220.         *x=xb;
  221.         *y=*y+1; } }
  222.         
  223. void south_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) { 
  224.     if(*y==yb) return;
  225.     gl_setpixel(*x,*y,pixel);
  226.     *x=*x-1;
  227.     if(*x==xb) {
  228.         *x=xm+xb;
  229.         *y=*y-1; } }        
  230.         
  231. void west_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  232.     if(*x>(ym+yb)) return;
  233.     gl_setpixel(*x,*y,pixel);
  234.     *y=*y-1;
  235.     if(*y==xb) {
  236.         *y=xb+xm;
  237.         *x=*x+1; } }
  238.         
  239. void east_pixel(int *x,int *y,int xm,int ym,int xb,int yb,int pixel) {
  240.     if((*x)<yb) return;
  241.     gl_setpixel(*x,*y,pixel);
  242.     *y=*y+1;
  243.     if(*y==(xm+xb)) {
  244.         *y=xb;
  245.         *x=*x-1; } }
  246.